package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/10 17:11
 * https://leetcode-cn.com/problems/surrounded-regions/
 */
public class Title130 {

    static int n, m;
    public static void solve(char[][] board) {
        n = board.length;
        m = board[0].length;
        //遍历四条边
        //左右
        for (int i = 0; i < n; i++) {
            dfs(board, i, 0);
            dfs(board, i, m - 1);
        }
        //上下
        for (int j = 0; j < m; j++) {
            dfs(board, 0, j);
            dfs(board, n - 1, j);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }

    /**
     * 判断
     * @param board
     * @param i
     * @param j
     * @return
     */
    private static void dfs(char[][] board, int i, int j) {
        //此时'A' 'X'跳过
        if (i < 0 || i >= n || j < 0 || j >= m || board[i][j] != 'O') {
            return;
        }
        //此时board[i][j] == 'O'
        board[i][j] = 'A';
        //找与边缘上的'O'相连的'O'
        dfs(board, i - 1, j);
        dfs(board, i, j - 1);
        dfs(board, i + 1, j);
        dfs(board, i, j + 1);
    }

    public static void main(String[] args) {
        char[][] temp = {{'X','O','O','X'},{'X','O','X','X'},{'X','X','X','X'},{'X','O','X','X'}};
        solve(temp);
    }
}
